*****************************************************************************
* Cleaning and Analyzing - PRITM countries. Polarization proxy as distance  *
*						Manifesto Project Database                          *
*  																            *
* Author: 			Valentina Gonzalez-Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			March 21 2021 									*
* Version:			Stata 17						                        *
*																			*
*****************************************************************************

/*
This do-file:
	A. Call the Data
	B. Define variables
	C. Export Tables 

Input: ** Manifesto Project database**
	- Data\CMP\MPDataset_MPDS2020a_stata14.dta // Data download from https://manifesto-project.wzb.eu/datasets 


Final output:
	Cleaned data: 
		* "Data\CMP_main.dta" this data contains the relevant variables for the analysis with the DV as the polarization over redistribution, and fixed-value positions, estimated as the distance between establishment left and outsider parties.
	Tables:
		* table 3: PRITM: Partisan Polarization over Redistribution and Fixed Attributes [Table\TabWithin.tex]
		* table A19: Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Of [Table\TabWithin_cutoff.tex]
		* table A20: Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist [Table\TabWithin_FValternative.tex]
		* table A18: Descriptive statistic: PRITM 1970-2019 [Table\desc_PRITM.tex]


*/
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication" // Only change your directory
* Processing of the data (alternatively skip and go to line 809)
{
*##########################################
* A. Calling the data
*##########################################
{

use "Data\CMP\MPDataset_MPDS2020a_stata14.dta", replace

}
*############################################
* B. Creating Variables
*############################################
{
* YEAR
gen year = year(edate) 

* Keep countries of interest
keep if countryname=="Australia" |  countryname=="Canada" |  countryname=="Greece" |  countryname=="New Zealand" |  countryname=="Portugal" |  countryname=="Spain" |  countryname=="United Kingdom" |  countryname=="United States" |  countryname=="France" |  countryname=="Norway" | countryname=="Austria" | countryname=="Belgium" | countryname=="Denmark" | countryname=="Estonia" | countryname=="Finland" | countryname=="Hungary" | countryname=="Germany" | countryname=="Iceland"  | countryname=="Ireland"  | countryname=="Italy"  | countryname=="Netherlands"  | countryname=="Norway" | countryname=="Slovakia" | countryname=="Slovenia" | countryname=="Sweden" | countryname=="Switzerland"

* Defines a dummy to identify PRITM countries
gen PRITM=. 
replace PRITM=0 if countryname=="Australia"
replace PRITM=0 if countryname=="Canada"
replace PRITM=0 if countryname=="Greece"
replace PRITM=0 if countryname=="New Zealand"
replace PRITM=0 if countryname=="Portugal"
replace PRITM=0 if countryname=="Spain"
replace PRITM=0 if countryname=="United Kingdom"
replace PRITM=0 if countryname=="United States"
replace PRITM=0 if countryname=="France"

replace PRITM=1 if countryname=="Austria"
replace PRITM=1 if countryname=="Belgium"
replace PRITM=1 if countryname=="Denmark"
replace PRITM=1 if countryname=="Estonia"
replace PRITM=1 if countryname=="Finland"
replace PRITM=1 if countryname=="Germany"
replace PRITM=1 if countryname=="Hungary"
replace PRITM=1 if countryname=="Iceland"
replace PRITM=1 if countryname=="Ireland"
replace PRITM=1 if countryname=="Italy"
replace PRITM=1 if countryname=="Netherlands"
replace PRITM=1 if countryname=="Norway"
replace PRITM=1 if countryname=="Slovenia"
replace PRITM=1 if countryname=="Slovakia"
replace PRITM=1 if countryname=="Sweden"
replace PRITM=1 if countryname=="Switzerland"


* CONTROL VARIABLES - number of parties

gen number = 1
egen number2= sum(number), by(edate)
lab var number2 "Number of parties"

*############################################################
* DEPENDENT VARIABLE PRITM
*############################################################
{
* Obtaining relevant policy variables
{
gen welfare_policy =  ln(per504+0.5) - ln(per505+0.5)

gen national_neg =  (ln(0.5 +per107)-ln(0.5 +per109)) // internationalism narrow

gen fixed = .
replace fixed = (ln(per107+per108+per407+per602+0.5+per602_2)-ln(per109+per110+per406+per601+0.5+per601_2)) if !missing(per602_2) & !missing(per601_2)
replace fixed = (ln(per107+per108+per407+per602+0.5)-ln(per109+per110+per406+per601+0.5)) if missing(per602_2) | missing(per601_2)




gen fixed_broad_policy = .
replace fixed_broad_policy = (ln(per107+per108+per407+per602+0.5 + per602_2+per604+per607)-ln(per109+per110+per406+per601+0.5 + per601_2+per603 +per608)) if !missing(per602_2) & !missing(per601_2)
replace fixed_broad_policy = (ln(per107+per108+per407+per602+0.5 +per604+per607)-ln(per109+per110+per406+per601+0.5 +per603 +per608)) if  missing(per602_2) | missing(per601_2)

gen fixed_eu_policy =  .
replace fixed_eu_policy =  (ln(per108+per602_2+0.5)-ln(per110+per601_2+0.5)) if !missing(per602_2) & !missing(per601_2)
replace fixed_eu_policy =  (ln(per108+0.5)-ln(per110+0.5)) if  missing(per602_2) | missing(per601_2)


gen fixed_nolog1_policy =  .
replace fixed_nolog1_policy=(per109-per107) + (per601 - per602) + (per603 - per604) + (per608 - per607) + (per601_2-per602_2) + (per406-per407)+(per110-per108) if !missing(per602_2) & !missing(per601_2)
replace fixed_nolog1_policy=(per109-per107) + (per601 - per602) + (per603 - per604) + (per608 - per607)  + (per406-per407)+(per110-per108) if missing(per602_2) | missing(per601_2) 
}
* DEFINE PARTY FAMILY GROUPS
{
**  Mainstream left: - soc social democratic and socialist or other left
gen m_left = . 
replace m_left = 1 if parfam== 30 | parfam== 20
replace m_left = 0 if parfam ~= 30 & parfam ~=20 & parfam ~=. 

egen m_left_participation= max(m_left), by(edate)


**  Mainstream left: only soc social democratic
gen m_left_restrict = . 
replace m_left_restrict = 1 if parfam== 30
replace m_left_restrict = 0 if parfam ~= 30 & parfam ~=. 

** Other left: Ecologista and socialist
gen o_left = . 
replace o_left = 1 if  parfam== 10  // here I am also including socialist or other left
replace o_left = 0 if parfam ~= 10  & parfam ~=. 

egen o_left_participation= max(o_left), by(edate)

** Mainstream right: lib liberal, Christian Democrat, Conservatives
gen m_right = . 
replace m_right = 1 if parfam == 40 | parfam == 50 | parfam == 60
replace m_right = 0 if parfam ~= 40 & parfam ~= 50 & parfam ~= 60 & parfam ~=. 

egen m_right_participation= max(m_right), by(edate)

** Other right: agriculture
gen o_right = . 
replace o_right = 1 if parfam == 80
replace o_right = 0 if parfam ~= 80 & parfam ~=. 

egen o_right_participation= max(o_right), by(edate)

** Radical right:  nat nationalist 
gen rad_right = . 
replace rad_right = 1 if parfam == 70 
replace rad_right = 0 if parfam ~= 70 & parfam ~=. 

egen rad_right_participation= max(rad_right), by(edate)


** Other parties: special issues and Ethnic and regional parties
gen o_parties = . 
replace o_parties = 1 if parfam == 90 | parfam == 95 
replace o_parties = 0 if parfam ~= 90 & parfam ~= 95  & parfam ~=. 

egen o_parties_participation= max(o_parties), by(edate)
}
** DISTANCE BY PARTY FAMILY
{

gen welfare_m_left = m_left*welfare_policy if m_left==1
gen welfare_rad_right = rad_right*welfare_policy if rad_right==1
gen welfare_m_right = m_right*welfare_policy if m_right==1

gen welfare_o_left = o_left*welfare_policy if o_left==1
gen welfare_o_right = o_right*welfare_policy if o_right==1
gen welfare_o_parties = o_parties*welfare_policy if o_parties==1

gen national_m_left = m_left*national_neg if m_left==1
gen national_rad_right = rad_right*national_neg if rad_right==1
gen national_m_right = m_right*national_neg if m_right==1

gen fixed_m_left = m_left*fixed if m_left==1
gen fixed_rad_right = rad_right*fixed if rad_right==1
gen fixed_m_right = m_right*fixed if m_right==1

gen fixed_o_left = o_left*fixed if o_left==1
gen fixed_o_right = o_right*fixed if o_right==1
gen fixed_o_parties = o_parties*fixed if o_parties==1


gen fixed_broad_m_left = m_left*fixed_broad_policy if m_left==1
gen fixed_broad_rad_right = rad_right*fixed_broad_policy if rad_right==1
gen fixed_broad_m_right = m_right*fixed_broad_policy if m_right==1

gen fixed_eu_m_left = m_left*fixed_eu_policy if m_left==1
gen fixed_eu_rad_right = rad_right*fixed_eu_policy if rad_right==1
gen fixed_eu_m_right = m_right*fixed_eu_policy if m_right==1

gen fixed_nolog1_m_left = m_left*fixed_nolog1_policy if m_left==1
gen fixed_nolog1_rad_right = rad_right*fixed_nolog1_policy if rad_right==1
gen fixed_nolog1_m_right = m_right*fixed_nolog1_policy if m_right==1


sort countryname edate

replace welfare_m_left = 0 if welfare_m_left ==.
replace welfare_rad_right = 0 if welfare_rad_right ==.
replace welfare_m_right = 0 if welfare_m_right ==.

replace national_m_left = 0 if national_m_left ==.
replace national_rad_right = 0 if national_rad_right ==.
replace national_m_right = 0 if national_m_right ==.


replace  fixed_m_left = 0 if  fixed_m_left ==.
replace  fixed_rad_right = 0 if  fixed_rad_right ==.
replace  fixed_m_right = 0 if  fixed_m_right ==.



replace fixed_broad_m_left = 0 if fixed_broad_m_left ==.
replace fixed_broad_rad_right = 0 if fixed_broad_rad_right ==.
replace fixed_broad_m_right = 0 if fixed_broad_m_right ==.

replace fixed_eu_m_left = 0 if fixed_eu_m_left ==.
replace fixed_eu_rad_right = 0 if fixed_eu_rad_right ==.
replace fixed_eu_m_right = 0 if fixed_eu_m_right ==.



replace fixed_nolog1_m_left = 0 if fixed_nolog1_m_left ==.
replace fixed_nolog1_rad_right = 0 if fixed_nolog1_rad_right ==.
replace fixed_nolog1_m_right = 0 if fixed_nolog1_m_right ==.


}	
* Collapsing the data 
{
collapse (sum) fixed_m_left fixed_m_right  fixed_rad_right   welfare_m_left welfare_rad_right welfare_m_right national_m_left national_rad_right national_m_right   fixed_broad_m_left fixed_broad_rad_right fixed_broad_m_right fixed_eu_m_left fixed_eu_rad_right fixed_eu_m_right fixed_nolog1_m_left fixed_nolog1_rad_right fixed_nolog1_m_right  (first) year  PRITM  totseats number2 oecdmember date *_participation , by(edate countryname)
}
* Final prep: absolute distance
{
gen distance_redist = abs(welfare_m_left-welfare_m_right)
replace distance_redist = abs(welfare_m_left-welfare_rad_right) if PRITM==1

gen distance_nat = abs(national_m_left-national_m_right)
replace distance_nat = abs(national_m_left-national_rad_right) if PRITM==1


gen distance_fixed = abs(fixed_m_left-fixed_m_right)
replace distance_fixed = abs(fixed_m_left-fixed_rad_right) if PRITM==1



gen distance_fixed_all = abs(fixed_broad_m_left-fixed_broad_m_right)
replace distance_fixed_all = abs(fixed_broad_m_left-fixed_broad_rad_right) if PRITM==1

gen distance_fixed_eu= abs(fixed_eu_m_left-fixed_eu_m_right)
replace distance_fixed_eu = abs(fixed_eu_m_left-fixed_eu_rad_right) if PRITM==1


gen distance_fixed_nolog= abs(fixed_nolog1_m_left-fixed_nolog1_m_right)
replace distance_fixed_nolog = abs(fixed_nolog1_m_left-fixed_nolog1_rad_right) if PRITM==1

}


}
// Final Prep of the data
{
sort countryname edate

// Define dummy for the period pre or post 1994 when LMP was high
gen shock=.
replace shock =0 if year > 1969 & year < 1995
replace shock =1 if  year > 1994

egen country_number = group(countryname)

sort country_number edate
bysort country_number: gen election_order=_n
sort countryname  election_order

xtset country_number election_order
	
}
*############################################################
* IFR - Alternative to the shock IV which is just  a dummy 
*############################################################
{
// This includes the data provided by IFR by year and country about the number of industrial robots. Data available from 2004 to 2019	
gen IFR=. 
*2004
{
replace IFR =0 if countryname =="Canada" & year ==2004
replace IFR =123663 if countryname =="Canada" & year ==2004
replace IFR =4170 if countryname =="Canada" & year ==2004
replace IFR =0 if countryname =="Canada" & year ==2004
replace IFR =391 if countryname =="Canada" & year ==2004
replace IFR =285 if countryname =="Canada" & year ==2004
replace IFR =458 if countryname =="Canada" & year ==2004
replace IFR =2 if countryname =="Canada" & year ==2004
replace IFR =3907 if countryname =="Canada" & year ==2004
replace IFR =5987 if countryname =="Canada" & year ==2004
replace IFR =120544 if countryname =="Canada" & year ==2004
replace IFR =21893 if countryname =="Canada" & year ==2004
replace IFR =28133 if countryname =="Canada" & year ==2004
replace IFR =53244 if countryname =="Canada" & year ==2004
replace IFR =2762 if countryname =="Canada" & year ==2004
replace IFR =1488 if countryname =="Canada" & year ==2004
replace IFR =3540 if countryname =="Canada" & year ==2004
replace IFR =14176 if countryname =="Canada" & year ==2004
replace IFR =2342 if countryname =="Canada" & year ==2004
replace IFR =3712 if countryname =="Canada" & year ==2004
replace IFR =724 if countryname =="Canada" & year ==2004
replace IFR =7341 if countryname =="Canada" & year ==2004
replace IFR =63 if countryname =="Canada" & year ==2004
replace IFR =6 if countryname =="Canada" & year ==2004
replace IFR =31 if countryname =="Canada" & year ==2004
}
*2005
{
replace IFR =0 if countryname =="Canada" & year ==2005
replace IFR =139984 if countryname =="United States" & year ==2005
replace IFR =4915 if countryname =="Australia" & year ==2005
replace IFR =23 if countryname =="New Zealand" & year ==2005
replace IFR =460 if countryname =="Slovenia" & year ==2005
replace IFR =458 if countryname =="Hungary" & year ==2005
replace IFR =576 if countryname =="Slovakia" & year ==2005
replace IFR =4 if countryname =="Estonia" & year ==2005
replace IFR =4148 if countryname =="Austria" & year ==2005
replace IFR =6124 if countryname =="Belgium" & year ==2005
replace IFR =126294 if countryname =="Germany" & year ==2005
replace IFR =24141 if countryname =="Spain" & year ==2005
replace IFR =30236 if countryname =="France" & year ==2005
replace IFR =56198 if countryname =="Italy" & year ==2005
replace IFR =3238 if countryname =="Netherlands" & year ==2005
replace IFR =1542 if countryname =="Portugal" & year ==2005
replace IFR =3732 if countryname =="Switzerland" & year ==2005
replace IFR =14948 if countryname =="United Kingdom" & year ==2005
replace IFR =2661 if countryname =="Denmark" & year ==2005
replace IFR =4159 if countryname =="Finland" & year ==2005
replace IFR =811 if countryname =="Norway" & year ==2005
replace IFR =8028 if countryname =="Sweden" & year ==2005
replace IFR =73 if countryname =="Greece" & year ==2005
replace IFR =7 if countryname =="Iceland" & year ==2005
replace IFR =121 if countryname =="Ireland" & year ==2005
	
}
*2006
{

replace IFR =0 if countryname =="Canada" & year ==2006
replace IFR =150725 if countryname =="United States" & year ==2006
replace IFR =5478 if countryname =="Australia" & year ==2006
replace IFR =76 if countryname =="New Zealand" & year ==2006
replace IFR =560 if countryname =="Slovenia" & year ==2006
replace IFR =592 if countryname =="Hungary" & year ==2006
replace IFR =596 if countryname =="Slovakia" & year ==2006
replace IFR =4 if countryname =="Estonia" & year ==2006
replace IFR =4382 if countryname =="Austria" & year ==2006
replace IFR =6331 if countryname =="Belgium" & year ==2006
replace IFR =132594 if countryname =="Germany" & year ==2006
replace IFR =26008 if countryname =="Spain" & year ==2006
replace IFR =32110 if countryname =="France" & year ==2006
replace IFR =58898 if countryname =="Italy" & year ==2006
replace IFR =3797 if countryname =="Netherlands" & year ==2006
replace IFR =1710 if countryname =="Portugal" & year ==2006
replace IFR =3940 if countryname =="Switzerland" & year ==2006
replace IFR =15082 if countryname =="United Kingdom" & year ==2006
replace IFR =3013 if countryname =="Denmark" & year ==2006
replace IFR =4349 if countryname =="Finland" & year ==2006
replace IFR =960 if countryname =="Norway" & year ==2006
replace IFR =8245 if countryname =="Sweden" & year ==2006
replace IFR =90 if countryname =="Greece" & year ==2006
replace IFR =13 if countryname =="Iceland" & year ==2006
replace IFR =191 if countryname =="Ireland" & year ==2006
	
}

*2007
{
	
replace IFR =0 if countryname =="Canada" & year ==2007
replace IFR =160632 if countryname =="United States" & year ==2007
replace IFR =5998 if countryname =="Australia" & year ==2007
replace IFR =129 if countryname =="New Zealand" & year ==2007
replace IFR =709 if countryname =="Slovenia" & year ==2007
replace IFR =772 if countryname =="Hungary" & year ==2007
replace IFR =677 if countryname =="Slovakia" & year ==2007
replace IFR =6 if countryname =="Estonia" & year ==2007
replace IFR =4761 if countryname =="Austria" & year ==2007
replace IFR =6301 if countryname =="Belgium" & year ==2007
replace IFR =139980 if countryname =="Germany" & year ==2007
replace IFR =27473 if countryname =="Spain" & year ==2007
replace IFR =33462 if countryname =="France" & year ==2007
replace IFR =61589 if countryname =="Italy" & year ==2007
replace IFR =4347 if countryname =="Netherlands" & year ==2007
replace IFR =1892 if countryname =="Portugal" & year ==2007
replace IFR =4215 if countryname =="Switzerland" & year ==2007
replace IFR =15340 if countryname =="United Kingdom" & year ==2007
replace IFR =3514 if countryname =="Denmark" & year ==2007
replace IFR =4495 if countryname =="Finland" & year ==2007
replace IFR =1012 if countryname =="Norway" & year ==2007
replace IFR =8830 if countryname =="Sweden" & year ==2007
replace IFR =144 if countryname =="Greece" & year ==2007
replace IFR =13 if countryname =="Iceland" & year ==2007
replace IFR =288 if countryname =="Ireland" & year ==2007

}
*2008
{
	
replace IFR =0 if countryname =="Canada" & year ==2008
replace IFR =168489 if countryname =="United States" & year ==2008
replace IFR =6529 if countryname =="Australia" & year ==2008
replace IFR =185 if countryname =="New Zealand" & year ==2008
replace IFR =852 if countryname =="Slovenia" & year ==2008
replace IFR =1014 if countryname =="Hungary" & year ==2008
replace IFR =860 if countryname =="Slovakia" & year ==2008
replace IFR =7 if countryname =="Estonia" & year ==2008
replace IFR =5122 if countryname =="Austria" & year ==2008
replace IFR =6276 if countryname =="Belgium" & year ==2008
replace IFR =144643 if countryname =="Germany" & year ==2008
replace IFR =28636 if countryname =="Spain" & year ==2008
replace IFR =34370 if countryname =="France" & year ==2008
replace IFR =63051 if countryname =="Italy" & year ==2008
replace IFR =4848 if countryname =="Netherlands" & year ==2008
replace IFR =1990 if countryname =="Portugal" & year ==2008
replace IFR =4431 if countryname =="Switzerland" & year ==2008
replace IFR =15080 if countryname =="United Kingdom" & year ==2008
replace IFR =3891 if countryname =="Denmark" & year ==2008
replace IFR =4663 if countryname =="Finland" & year ==2008
replace IFR =1031 if countryname =="Norway" & year ==2008
replace IFR =9426 if countryname =="Sweden" & year ==2008
replace IFR =203 if countryname =="Greece" & year ==2008
replace IFR =13 if countryname =="Iceland" & year ==2008
replace IFR =340 if countryname =="Ireland" & year ==2008

}
*2009
{
	
replace IFR =0 if countryname =="Canada" & year ==2009
replace IFR =166183 if countryname =="United States" & year ==2009
replace IFR =6402 if countryname =="Australia" & year ==2009
replace IFR =295 if countryname =="New Zealand" & year ==2009
replace IFR =903 if countryname =="Slovenia" & year ==2009
replace IFR =1207 if countryname =="Hungary" & year ==2009
replace IFR =1068 if countryname =="Slovakia" & year ==2009
replace IFR =32 if countryname =="Estonia" & year ==2009
replace IFR =5398 if countryname =="Austria" & year ==2009
replace IFR =6448 if countryname =="Belgium" & year ==2009
replace IFR =144133 if countryname =="Germany" & year ==2009
replace IFR =28781 if countryname =="Spain" & year ==2009
replace IFR =34099 if countryname =="France" & year ==2009
replace IFR =62242 if countryname =="Italy" & year ==2009
replace IFR =5230 if countryname =="Netherlands" & year ==2009
replace IFR =2144 if countryname =="Portugal" & year ==2009
replace IFR =4377 if countryname =="Switzerland" & year ==2009
replace IFR =13923 if countryname =="United Kingdom" & year ==2009
replace IFR =4076 if countryname =="Denmark" & year ==2009
replace IFR =4719 if countryname =="Finland" & year ==2009
replace IFR =1025 if countryname =="Norway" & year ==2009
replace IFR =9396 if countryname =="Sweden" & year ==2009
replace IFR =242 if countryname =="Greece" & year ==2009
replace IFR =13 if countryname =="Iceland" & year ==2009
replace IFR =370 if countryname =="Ireland" & year ==2009

}
* 2010
{
	
replace IFR =0 if countryname =="Canada" & year ==2010
replace IFR =173174 if countryname =="United States" & year ==2010
replace IFR =6679 if countryname =="Australia" & year ==2010
replace IFR =387 if countryname =="New Zealand" & year ==2010
replace IFR =1032 if countryname =="Slovenia" & year ==2010
replace IFR =1406 if countryname =="Hungary" & year ==2010
replace IFR =1870 if countryname =="Slovakia" & year ==2010
replace IFR =36 if countryname =="Estonia" & year ==2010
replace IFR =5749 if countryname =="Austria" & year ==2010
replace IFR =6251 if countryname =="Belgium" & year ==2010
replace IFR =148256 if countryname =="Germany" & year ==2010
replace IFR =28868 if countryname =="Spain" & year ==2010
replace IFR =34495 if countryname =="France" & year ==2010
replace IFR =62378 if countryname =="Italy" & year ==2010
replace IFR =5438 if countryname =="Netherlands" & year ==2010
replace IFR =2280 if countryname =="Portugal" & year ==2010
replace IFR =4417 if countryname =="Switzerland" & year ==2010
replace IFR =13519 if countryname =="United Kingdom" & year ==2010
replace IFR =4234 if countryname =="Denmark" & year ==2010
replace IFR =4611 if countryname =="Finland" & year ==2010
replace IFR =1012 if countryname =="Norway" & year ==2010
replace IFR =9387 if countryname =="Sweden" & year ==2010
replace IFR =286 if countryname =="Greece" & year ==2010
replace IFR =13 if countryname =="Iceland" & year ==2010
replace IFR =411 if countryname =="Ireland" & year ==2010

}
*2011
{
	
replace IFR =1848 if countryname =="Canada" & year ==2011
replace IFR =180893 if countryname =="United States" & year ==2011
replace IFR =7189 if countryname =="Australia" & year ==2011
replace IFR =510 if countryname =="New Zealand" & year ==2011
replace IFR =1194 if countryname =="Slovenia" & year ==2011
replace IFR =2347 if countryname =="Hungary" & year ==2011
replace IFR =2210 if countryname =="Slovakia" & year ==2011
replace IFR =47 if countryname =="Estonia" & year ==2011
replace IFR =6104 if countryname =="Austria" & year ==2011
replace IFR =6243 if countryname =="Belgium" & year ==2011
replace IFR =157241 if countryname =="Germany" & year ==2011
replace IFR =29847 if countryname =="Spain" & year ==2011
replace IFR =34461 if countryname =="France" & year ==2011
replace IFR =62245 if countryname =="Italy" & year ==2011
replace IFR =6108 if countryname =="Netherlands" & year ==2011
replace IFR =2372 if countryname =="Portugal" & year ==2011
replace IFR =4717 if countryname =="Switzerland" & year ==2011
replace IFR =13641 if countryname =="United Kingdom" & year ==2011
replace IFR =4417 if countryname =="Denmark" & year ==2011
replace IFR =4473 if countryname =="Finland" & year ==2011
replace IFR =1025 if countryname =="Norway" & year ==2011
replace IFR =9781 if countryname =="Sweden" & year ==2011
replace IFR =290 if countryname =="Greece" & year ==2011
replace IFR =13 if countryname =="Iceland" & year ==2011
replace IFR =459 if countryname =="Ireland" & year ==2011

}
*2012
{
	
replace IFR =3597 if countryname =="Canada" & year ==2012
replace IFR =190321 if countryname =="United States" & year ==2012
replace IFR =7963 if countryname =="Australia" & year ==2012
replace IFR =620 if countryname =="New Zealand" & year ==2012
replace IFR =1474 if countryname =="Slovenia" & year ==2012
replace IFR =3301 if countryname =="Hungary" & year ==2012
replace IFR =2294 if countryname =="Slovakia" & year ==2012
replace IFR =51 if countryname =="Estonia" & year ==2012
replace IFR =6619 if countryname =="Austria" & year ==2012
replace IFR =6890 if countryname =="Belgium" & year ==2012
replace IFR =161988 if countryname =="Germany" & year ==2012
replace IFR =28911 if countryname =="Spain" & year ==2012
replace IFR =33624 if countryname =="France" & year ==2012
replace IFR =60750 if countryname =="Italy" & year ==2012
replace IFR =6718 if countryname =="Netherlands" & year ==2012
replace IFR =2524 if countryname =="Portugal" & year ==2012
replace IFR =5010 if countryname =="Switzerland" & year ==2012
replace IFR =15046 if countryname =="United Kingdom" & year ==2012
replace IFR =4613 if countryname =="Denmark" & year ==2012
replace IFR =4311 if countryname =="Finland" & year ==2012
replace IFR =1019 if countryname =="Norway" & year ==2012
replace IFR =9824 if countryname =="Sweden" & year ==2012
replace IFR =311 if countryname =="Greece" & year ==2012
replace IFR =13 if countryname =="Iceland" & year ==2012
replace IFR =522 if countryname =="Ireland" & year ==2012

}
*2013
{

replace IFR =5847 if countryname =="Canada" & year ==2013
replace IFR =203187 if countryname =="United States" & year ==2013
replace IFR =8016 if countryname =="Australia" & year ==2013
replace IFR =770 if countryname =="New Zealand" & year ==2013
replace IFR =1606 if countryname =="Slovenia" & year ==2013
replace IFR =3829 if countryname =="Hungary" & year ==2013
replace IFR =3572 if countryname =="Slovakia" & year ==2013
replace IFR =66 if countryname =="Estonia" & year ==2013
replace IFR =7009 if countryname =="Austria" & year ==2013
replace IFR =7998 if countryname =="Belgium" & year ==2013
replace IFR =167579 if countryname =="Germany" & year ==2013
replace IFR =28091 if countryname =="Spain" & year ==2013
replace IFR =32301 if countryname =="France" & year ==2013
replace IFR =59078 if countryname =="Italy" & year ==2013
replace IFR =7403 if countryname =="Netherlands" & year ==2013
replace IFR =2666 if countryname =="Portugal" & year ==2013
replace IFR =5270 if countryname =="Switzerland" & year ==2013
replace IFR =15591 if countryname =="United Kingdom" & year ==2013
replace IFR =4760 if countryname =="Denmark" & year ==2013
replace IFR =4268 if countryname =="Finland" & year ==2013
replace IFR =1005 if countryname =="Norway" & year ==2013
replace IFR =10164 if countryname =="Sweden" & year ==2013
replace IFR =344 if countryname =="Greece" & year ==2013
replace IFR =20 if countryname =="Iceland" & year ==2013
replace IFR =605 if countryname =="Ireland" & year ==2013
	
}
* 2014
{
	
replace IFR =8180 if countryname =="Canada" & year ==2014
replace IFR =219434 if countryname =="United States" & year ==2014
replace IFR =7927 if countryname =="Australia" & year ==2014
replace IFR =864 if countryname =="New Zealand" & year ==2014
replace IFR =1819 if countryname =="Slovenia" & year ==2014
replace IFR =4302 if countryname =="Hungary" & year ==2014
replace IFR =3891 if countryname =="Slovakia" & year ==2014
replace IFR =83 if countryname =="Estonia" & year ==2014
replace IFR =7237 if countryname =="Austria" & year ==2014
replace IFR =7995 if countryname =="Belgium" & year ==2014
replace IFR =175768 if countryname =="Germany" & year ==2014
replace IFR =27983 if countryname =="Spain" & year ==2014
replace IFR =32233 if countryname =="France" & year ==2014
replace IFR =59823 if countryname =="Italy" & year ==2014
replace IFR =8470 if countryname =="Netherlands" & year ==2014
replace IFR =2870 if countryname =="Portugal" & year ==2014
replace IFR =5764 if countryname =="Switzerland" & year ==2014
replace IFR =16935 if countryname =="United Kingdom" & year ==2014
replace IFR =5119 if countryname =="Denmark" & year ==2014
replace IFR =4178 if countryname =="Finland" & year ==2014
replace IFR =1008 if countryname =="Norway" & year ==2014
replace IFR =10742 if countryname =="Sweden" & year ==2014
replace IFR =392 if countryname =="Greece" & year ==2014
replace IFR =22 if countryname =="Iceland" & year ==2014
replace IFR =667 if countryname =="Ireland" & year ==2014

}
* 2015
{
	
replace IFR =11654 if countryname =="Canada" & year ==2015
replace IFR =234245 if countryname =="United States" & year ==2015
replace IFR =7742 if countryname =="Australia" & year ==2015
replace IFR =990 if countryname =="New Zealand" & year ==2015
replace IFR =2080 if countryname =="Slovenia" & year ==2015
replace IFR =4784 if countryname =="Hungary" & year ==2015
replace IFR =4378 if countryname =="Slovakia" & year ==2015
replace IFR =97 if countryname =="Estonia" & year ==2015
replace IFR =7859 if countryname =="Austria" & year ==2015
replace IFR =7989 if countryname =="Belgium" & year ==2015
replace IFR =182632 if countryname =="Germany" & year ==2015
replace IFR =29718 if countryname =="Spain" & year ==2015
replace IFR =32161 if countryname =="France" & year ==2015
replace IFR =61282 if countryname =="Italy" & year ==2015
replace IFR =9739 if countryname =="Netherlands" & year ==2015
replace IFR =3160 if countryname =="Portugal" & year ==2015
replace IFR =6258 if countryname =="Switzerland" & year ==2015
replace IFR =17469 if countryname =="United Kingdom" & year ==2015
replace IFR =5459 if countryname =="Denmark" & year ==2015
replace IFR =4124 if countryname =="Finland" & year ==2015
replace IFR =1068 if countryname =="Norway" & year ==2015
replace IFR =11857 if countryname =="Sweden" & year ==2015
replace IFR =446 if countryname =="Greece" & year ==2015
replace IFR =23 if countryname =="Iceland" & year ==2015
replace IFR =763 if countryname =="Ireland" & year ==2015

}
* 2016
{
	
replace IFR =13988 if countryname =="Canada" & year ==2016
replace IFR =250479 if countryname =="United States" & year ==2016
replace IFR =7536 if countryname =="Australia" & year ==2016
replace IFR =1105 if countryname =="New Zealand" & year ==2016
replace IFR =2452 if countryname =="Slovenia" & year ==2016
replace IFR =5424 if countryname =="Hungary" & year ==2016
replace IFR =6071 if countryname =="Slovakia" & year ==2016
replace IFR =123 if countryname =="Estonia" & year ==2016
replace IFR =9000 if countryname =="Austria" & year ==2016
replace IFR =8521 if countryname =="Belgium" & year ==2016
replace IFR =189305 if countryname =="Germany" & year ==2016
replace IFR =30811 if countryname =="Spain" & year ==2016
replace IFR =33384 if countryname =="France" & year ==2016
replace IFR =62068 if countryname =="Italy" & year ==2016
replace IFR =11320 if countryname =="Netherlands" & year ==2016
replace IFR =3942 if countryname =="Portugal" & year ==2016
replace IFR =6753 if countryname =="Switzerland" & year ==2016
replace IFR =18471 if countryname =="United Kingdom" & year ==2016
replace IFR =5915 if countryname =="Denmark" & year ==2016
replace IFR =4422 if countryname =="Finland" & year ==2016
replace IFR =1173 if countryname =="Norway" & year ==2016
replace IFR =12671 if countryname =="Sweden" & year ==2016
replace IFR =491 if countryname =="Greece" & year ==2016
replace IFR =31 if countryname =="Iceland" & year ==2016
replace IFR =880 if countryname =="Ireland" & year ==2016

}

*2017
{

replace IFR =18045 if countryname =="Canada" & year ==2017
replace IFR =262058 if countryname =="United States" & year ==2017
replace IFR =7126 if countryname =="Australia" & year ==2017
replace IFR =1172 if countryname =="New Zealand" & year ==2017
replace IFR =2805 if countryname =="Slovenia" & year ==2017
replace IFR =7711 if countryname =="Hungary" & year ==2017
replace IFR =7093 if countryname =="Slovakia" & year ==2017
replace IFR =170 if countryname =="Estonia" & year ==2017
replace IFR =10156 if countryname =="Austria" & year ==2017
replace IFR =9207 if countryname =="Belgium" & year ==2017
replace IFR =200497 if countryname =="Germany" & year ==2017
replace IFR =32352 if countryname =="Spain" & year ==2017
replace IFR =35321 if countryname =="France" & year ==2017
replace IFR =64403 if countryname =="Italy" & year ==2017
replace IFR =12505 if countryname =="Netherlands" & year ==2017
replace IFR =4622 if countryname =="Portugal" & year ==2017
replace IFR =7476 if countryname =="Switzerland" & year ==2017
replace IFR =19488 if countryname =="United Kingdom" & year ==2017
replace IFR =6361 if countryname =="Denmark" & year ==2017
replace IFR =4342 if countryname =="Finland" & year ==2017
replace IFR =1250 if countryname =="Norway" & year ==2017
replace IFR =13249 if countryname =="Sweden" & year ==2017
replace IFR =568 if countryname =="Greece" & year ==2017
replace IFR =42 if countryname =="Iceland" & year ==2017
replace IFR =945 if countryname =="Ireland" & year ==2017
	
}
*2018
{

replace IFR =21627 if countryname =="Canada" & year ==2018
replace IFR =285014 if countryname =="United States" & year ==2018
replace IFR =6927 if countryname =="Australia" & year ==2018
replace IFR =1220 if countryname =="New Zealand" & year ==2018
replace IFR =3414 if countryname =="Slovenia" & year ==2018
replace IFR =8481 if countryname =="Hungary" & year ==2018
replace IFR =7796 if countryname =="Slovakia" & year ==2018
replace IFR =220 if countryname =="Estonia" & year ==2018
replace IFR =11162 if countryname =="Austria" & year ==2018
replace IFR =9561 if countryname =="Belgium" & year ==2018
replace IFR =215795 if countryname =="Germany" & year ==2018
replace IFR =35209 if countryname =="Spain" & year ==2018
replace IFR =38079 if countryname =="France" & year ==2018
replace IFR =69142 if countryname =="Italy" & year ==2018
replace IFR =13385 if countryname =="Netherlands" & year ==2018
replace IFR =5050 if countryname =="Portugal" & year ==2018
replace IFR =8492 if countryname =="Switzerland" & year ==2018
replace IFR =20683 if countryname =="United Kingdom" & year ==2018
replace IFR =6617 if countryname =="Denmark" & year ==2018
replace IFR =4553 if countryname =="Finland" & year ==2018
replace IFR =1219 if countryname =="Norway" & year ==2018
replace IFR =13647 if countryname =="Sweden" & year ==2018
replace IFR =640 if countryname =="Greece" & year ==2018
replace IFR =37 if countryname =="Iceland" & year ==2018
replace IFR =1026 if countryname =="Ireland" & year ==2018

	
}
* 2019
{
replace IFR =25230 if countryname =="Canada" & year ==2019
replace IFR =299674 if countryname =="United States" & year ==2019
replace IFR =6649 if countryname =="Australia" & year ==2019
replace IFR =1278 if countryname =="New Zealand" & year ==2019
replace IFR =3941 if countryname =="Slovenia" & year ==2019
replace IFR =9212 if countryname =="Hungary" & year ==2019
replace IFR =8326 if countryname =="Slovakia" & year ==2019
replace IFR =283 if countryname =="Estonia" & year ==2019
replace IFR =12016 if countryname =="Austria" & year ==2019
replace IFR =10109 if countryname =="Belgium" & year ==2019
replace IFR =223387 if countryname =="Germany" & year ==2019
replace IFR =36916 if countryname =="Spain" & year ==2019
replace IFR =42054 if countryname =="France" & year ==2019
replace IFR =74420 if countryname =="Italy" & year ==2019
replace IFR =14370 if countryname =="Netherlands" & year ==2019
replace IFR =5620 if countryname =="Portugal" & year ==2019
replace IFR =9506 if countryname =="Switzerland" & year ==2019
replace IFR =21678 if countryname =="United Kingdom" & year ==2019
replace IFR =6824 if countryname =="Denmark" & year ==2019
replace IFR =4728 if countryname =="Finland" & year ==2019
replace IFR =1271 if countryname =="Norway" & year ==2019
replace IFR =14224 if countryname =="Sweden" & year ==2019
replace IFR =665 if countryname =="Greece" & year ==2019
replace IFR =37 if countryname =="Iceland" & year ==2019
replace IFR =1130 if countryname =="Ireland" & year ==2019
	
}

// Now the log of the number of industrial robots
gen IFR2 = ln(IFR)
}
}

*############################################
* Saving the data
*############################################
{
lab var IFR2 "Robots Stock"
lab var PRITM "PR with Trichotomous Multipartism"
lab var totseats "Total Number of Seats"
lab var number2 "Total Number of Parties"
lab var oecdmember "OECD member"
lab var distance_redist "Distance Redistribution (DR) - Net Welfare"
lab var distance_fixed "Distance Fixed-Value Positions (DFVP) - Net Anti-Global"
lab var IFR2 "Robots Stock"

lab var distance_fixed_eu "DFVP - Net Anti-EU"
lab var distance_nat "DFVP - Net Anti-Global Narrow (Internationalism)"
lab var distance_fixed_all "DFVP - Anti-Global and Cultural"

lab var shock "High LMP period"

keep  PRITM year  countryname  oecdmember totseats shock distance_redist distance_fixed  distance_fixed_eu  distance_nat  distance_fixed_all distance_fixed_nolog IFR IFR2 number2 election_order country_number

keep if year>1969
keep if PRITM==1

save "Data\CMP_main.dta", replace
}
}
* Alternatively load prepared data
{
use "Data\CMP_main.dta", clear	
}
*##########################################
* Analysis 
*##########################################
{
// table 3: PRITM: Partisan Polarization over Redistribution and Fixed Attributes
{
preserve

eststo clear
eststo: qui reg distance_redist L.distance_redist shock totseats   oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_redist L.distance_redist IFR2 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed IFR2 totseats  oecdmember   i.year, cluster(countryname)



esttab , replace label se title(Polarization over Redistribution and Fixed Attributes between Mainstream Left and Right-Populist \label {TableCMPAllFE}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock    IFR2 ) scalars(N r2 aic) indicate("LDV = L.*" "FE Year = *year") 

esttab using "Table\TabWithin.tex", replace label se title(Polarization over Redistribution and Fixed Attributes  \label {TableCMPAll}) mti("Redistribution" "Fixed Values" "Redistribution" "Fixed Values") compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock  IFR2) scalars(N r2 aic) indicate("LDV = L.*" "FE Year = *year")

restore

}
// table A19: Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Of
{
gen shock92=.
replace shock92 =0 if year > 1969 & year < 1993
replace shock92 =1 if  year > 1992

gen shock93=.
replace shock93 =0 if year > 1969 & year < 1994
replace shock93 =1 if  year > 1993

gen shock96=.
replace shock96 =0 if year > 1969 & year < 1996
replace shock96 =1 if  year > 1997

gen shock97=.
replace shock97 =0 if year > 1969 & year < 1997
replace shock97 =1 if  year > 1998

gen shock98=.
replace shock98 =0 if year > 1969 & year < 1998
replace shock98 =1 if  year > 1999


lab var shock98 "Post-LMP"
lab var shock92 "Post-LMP"
lab var shock93 "Post-LMP"
lab var shock96 "Post-LMP"
lab var shock97 "Post-LMP"


preserve
  
eststo clear
eststo: qui reg distance_redist L.distance_redist shock92 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock92 totseats  oecdmember   i.year, cluster(countryname)

eststo: qui reg distance_redist L.distance_redist shock93 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock93 totseats  oecdmember   i.year, cluster(countryname)


eststo: qui reg distance_redist L.distance_redist shock96 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock96 totseats  oecdmember   i.year, cluster(countryname)


eststo: qui reg distance_redist L.distance_redist shock97 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock97 totseats  oecdmember   i.year, cluster(countryname)


eststo: qui reg distance_redist L.distance_redist shock98 totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg distance_fixed L.distance_fixed shock98 totseats  oecdmember   i.year, cluster(countryname)

esttab , replace label se title(Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Off \label {Tablewithincut}) mti("Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock*     ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )  indicate("LDV = L.*" "FE Year = *year")

esttab using "Table\TabWithin_cutoff.tex", replace label se title(Partisan Polarization over Redistribution and Fixed Attributes Different Cut-Off \label {Tablewithincut}) mti("Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" "Redist" "Fixed" ) compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock*     ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )  indicate("LDV = L.*" "FE Year = *year")

restore	
}
// table A20: Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist
{
	lab var distance_fixed_eu "Anti-EU"
lab var distance_nat "Internationalism"
lab var distance_fixed_all "Anti-Global and Cultural"
lab var distance_fixed_nolog "Anti-Global and Cultural (no log)"

preserve

eststo clear

foreach x of varlist distance_fixed_eu  distance_nat  distance_fixed_all distance_fixed_nolog {
eststo: qui reg `x' L.`x' shock totseats  oecdmember   i.year, cluster(countryname)
eststo: qui reg `x' L.`x' IFR2 totseats  oecdmember   i.year, cluster(countryname)

}
esttab, replace label se title(Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist \label {FVPritm})  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock IFR2 ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tot*" "LDV = L.*" "FE Year = *year") 

esttab using "Table\TabWithin_FValternative.tex", replace label se title(Alternative measures of Partisan Polarization over Fixed Attributes between Mainstream Left and Right-Populist \label {FVPritm})  compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(shock IFR2 ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) indicate("Controls = tot*" "LDV = L.*" "FE Year = *year") 

restore

}
}
*##########################################
* Descriptive 
*##########################################
// table A18: Descriptive statistic: PRITM 1970-2019
{
	
lab var distance_fixed_eu "DFVP - Net Anti-EU"
lab var distance_nat "DFVP - Net Anti-Global Narrow (Internationalism)"
lab var distance_fixed_all "DFVP - Anti-Global and Cultural"
lab var distance_fixed_nolog "DFVP - Anti-Global and Cultural (no log)"
	
lab var IFR "\# Robot Stock (IFR)"
lab var IFR2 "Ln \# Robot Stock (IFR)"
lab var distance_fixed_nolog "DFVP - Anti-Global and Cultural (no log)"
preserve

	eststo clear

qui estpost sum  totseats number2 oecdmember distance_redist  distance_fixed  distance_fixed_eu   distance_fixed_all distance_nat  distance_fixed_all distance_fixed_nolog IFR IFR2, d 


esttab ,cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs  title(Descriptive statistic: PRITM 1970-2019 \label {SummarystatPRITM}) 
	
esttab using "Table\desc_PRITM.tex",cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(0)) max(label(Max) fmt(0)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs  title(Descriptive statistic: PRITM 1970-2019 \label {SummarystatPRITM}) 
	
	restore
	}